﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XiMiModelDto;
using Microsoft.Extensions.Caching.Distributed;
using XiMiModel;
using Newtonsoft.Json;


namespace XiMiDal
{
    public class ShopDal
    {
        private readonly AppDbContext _appDbContext;
        private readonly IDistributedCache _cache;
        public ShopDal(AppDbContext appDbContext,IDistributedCache cache)
        {
            _appDbContext = appDbContext;
            _cache = cache;
        }
        public int AddShop(ShopModel shopModel)
        {
            //List<ShopModel> list = new List<ShopModel>();
            //var car = _cache.GetString("car");
            //if(string.IsNullOrWhiteSpace(car))
            //{
            //    list.Add(shopModel);
            //    _cache.SetString("car", JsonConvert.SerializeObject(list));
            //}
            //else
            //{
            //    list = JsonConvert.DeserializeObject<List<ShopModel>>(car);
            //    var item = list.Where(m => m.SId == shopModel.SId).FirstOrDefault();
            //    item.SNum += shopModel.SNum;
            //    _cache.SetString("car", JsonConvert.SerializeObject(list));
            //}
            //return 1;
            List<ShopDal> list = new List<ShopDal>();
            var sss = _cache.GetString("sss");
            if(sss==null)
            {
                sss = "abcd";
            }
            else
            {

            }
            return 1;
        }
        public List<ShopDtoModel> Show(out int totalcount)
        {
            var query = from a in _appDbContext.Shops
                        join c in _appDbContext.Goodss on a.GId equals c.GId
                        select new ShopDtoModel
                        {
                            SId = a.SId,
                            SNum = a.SNum,
                            UId = a.UId,
                            GId = c.GId,
                            GImg = "http://localhost:20338/StaticFiles/" + c.GImg,
                            GName = c.GName,
                            GPrice = c.GPrice,
                            GNum=c.GNum
                        };
            totalcount = query.Count();
            return query.ToList();
        }
        public int Del(int id)
        {
            var kk= _appDbContext.Shops.FirstOrDefault(m => m.SId == id);
            _appDbContext.Shops.Remove(kk);
            return _appDbContext.SaveChanges();
        }
        //public long AddOrder(OrderModel orderModel)
        //{
        //    var worker = Snow._idWorker;
        //    long id = worker.NextId();
        //    orderModel.ONumber = id;
        //    _appDbContext.Orders.Add(orderModel);
        //    return _appDbContext.SaveChanges();
        //}
        public int AddOrder(OrderFromDto orderFromDto,out string number)
        {
            var worker = Snow._idWorker;
            var id = worker.NextId();
            orderFromDto.orderModel.ONumber = id.ToString();
            _appDbContext.Orders.Add(orderFromDto.orderModel);
            foreach (var item in orderFromDto.ordersModels)
            {
                //循环给子表外键赋值
                item.ONumber = id.ToString();
                //循环添加子表数据
                _appDbContext.Orderss.Add(item);
            }
            number = id.ToString();
            return _appDbContext.SaveChanges();
        }
        public int AddCity(AddressModel addressModel)
        {
            _appDbContext.Addresss.Add(addressModel);
            return _appDbContext.SaveChanges();
        }
        public List<FixDtoModel> Check(int id)
        {
            var query = from a in _appDbContext.Users
                        join b in _appDbContext.Addresss on a.AId equals b.AId
                        join e in _appDbContext.Citys on b.PId equals e.PId
                        join c in _appDbContext.Citys on b.CId equals c.PId
                        join d in _appDbContext.Citys on b.TId equals d.PId
                        where a.UId==id
                        select new FixDtoModel
                        {
                            UId = a.UId,
                            AId = b.AId,
                            AName = b.AName,
                            AddressName = b.AddressName,
                            Phone = b.Phone,
                            Remark = b.Remark,
                            You = b.You,
                            PId = e.PId,
                            PName = e.PName,
                            CId = c.CId,
                            CName = c.PName,
                            TId = d.PId,
                            TName = d.PName
                        };
            return query.ToList();
        }
        public int DelAddress(int id)
        {
            var kk=_appDbContext.Addresss.FirstOrDefault(m => m.AId == id);
            _appDbContext.Addresss.Remove(kk);
            return _appDbContext.SaveChanges();
        }
        public int Update(AddressModel addressModel)
        {
            _appDbContext.Entry(addressModel).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            return _appDbContext.SaveChanges();
        }
        public List<CityModel> BindCity(int id)
        {
            return _appDbContext.Citys.Where(m => m.CId == id).ToList();
        }
        public List<ShowAddressDto> ShowAddressDto()
        {
            var query = from a in _appDbContext.Addresss
                        join b in _appDbContext.Citys on a.PId equals b.PId
                        join c in _appDbContext.Citys on a.CId equals c.PId
                        join d in _appDbContext.Citys on a.TId equals d.PId
                        select new ShowAddressDto
                        {
                            AId = a.AId,
                            AName = a.AName,
                            AddressName = a.AddressName,
                            Phone = a.Phone,
                            Remark = a.Remark,
                            You = a.You,
                            PId = b.PId,
                            PName = b.PName,
                            CId = c.CId,
                            CName = c.PName,
                            TId = d.PId,
                            TName = d.PName
                        };
            return query.ToList();
        }
        public AddressModel Fan(int id)
        {
           return _appDbContext.Addresss.FirstOrDefault(m => m.AId == id);
        }
    }
}
